<?php

namespace catchAdmin\apidoctor\service;

use catcher\exceptions\FailedException;
use app\common\model\WardmateCircle as WcircleModel;
use app\common\model\PatientInfo as PatientModel;

class CircleService
{
    /**
     * 获取群聊基本信息
     * @param string $id 群聊主键ID
     */
    public function getGroupChatInfo(string $id)
    {
        $info = (new WcircleModel())->getOneData(['id' => $id], "title,notice");

        if (empty($info))
        {
            throw new FailedException("群聊不存在");
        }

        //群公告是否为空
        $info['notice'] = isset($info['notice'])? $info['notice']: "";
        
        return  $info;
    }

    /**
     * 加入群聊
     * @param string $id 群聊主键ID
     * @param array $param 参数
     * @return bool
     */
    public function addGroupChat(string $id, array $param=[]): bool
    {
        $model = (new WcircleModel());

        $patientIds = [];
        foreach ($param as $key => $val)
        {
            $patientIds[] = $val['patientId'];
        }

        $is_exist = $model->where('id', $id)->whereIn('patientList.patientId', $patientIds)->select()->toArray();
            
        //患者是否加入过该群聊
        if (!empty($is_exist))
        {
            throw new FailedException("所选患者已加入过该群聊，不可重复加入");
        }

        return  $model->appToData($id, 'patientList', $param);
    }

    /**
     * 解散群聊
     * @param string $id 群聊主键ID
     * @param string $doctor_code 医生编号
     * @return bool
     */
    public function delGroupChat(string $id, string $doctor_code): bool
    {
        $model = (new WcircleModel());

        $condition['id'] = $id;
        $condition['doctor_code'] = $doctor_code;

        $is_exist = $model->getOneData($condition, 'id');

        if (empty($is_exist))
        {
            throw new FailedException("您不可解散该群聊");
        }
        return  $model->delData($id);
    }

    /** 
     * 获取成员列表 
     * @param string $id 群聊主键ID
     * @param string $doctor_code 医生编号 
     * @return array 
     */ 
    public function getParts(string $id ,string $doctor_code): array 
    { 
        $wcircleModel = new WcircleModel(); 
        $patientModel = new PatientModel(); 

        //获取已加入本群聊的成员
        $joined = $wcircleModel->getOneData(['id' => $id], 'patientList');

        $patientIds = [];
        if (isset($joined['patientList']) && !empty($joined['patientList'])) {
            $patientIds = array_column($joined['patientList'], 'patientId');
        }

        return $patientModel->whereOr("outList.attendingDoctorId", $doctor_code)
                ->whereOr("inList.inchargeDoctorNo", $doctor_code) 
                ->whereOr("hoList.inchargeDoctorNo", $doctor_code)
                ->whereNotIn("patientId", $patientIds) 
                ->field("patientId,name") 
                ->select()
                ->toArray();
    }

    /** 
     * 获取已加入的成员列表 
     * @param string $id 群聊主键ID
     * @return array 
     */
    public function getJoinedMembers(string $id): array
    {
        $wcircleModel = new WcircleModel(); 
        $patientModel = new PatientModel(); 

        $joined     = $wcircleModel->getOneData(['id' => $id], 'patientList'); 
        $patientIds = array_column($joined['patientList'], 'patientId');
        $member     = $patientModel->getNoSearchData([['patientId', 'in', $patientIds]], 'patientId,name,sex');
        return  $member;
    }

    /** 
     * 移除已加入的成员
     * @param string $id 群聊主键ID
     * @param string $doctor_code 医生编号 
     * @param array $patientId 患者id
     * @return bool 
     */
    public function removeMembers(string $id, string $doctor_code, array $patientId): bool
    {
        
        $wcircleModel = new WcircleModel();

        $is_exist = $wcircleModel->getOneData(['id' => $id,'doctor_code' => $doctor_code], 'id');
        if (empty($is_exist))
        {
            throw new FailedException("您没有权限移除成员");
        }
        
        foreach ($patientId as $key => $val)
        {
            $wcircleModel->removeToData($id, 'patientList', 'patientId', $val);
        }
        
        return  true;
    }
}